package servlet;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 石小俊
 */
@WebServlet("/findAll")
public class EmpListServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();
//        out.println(" <!DOCTYPE html> ");
//        out.println(" <html lang=\"en\"> ");
//        out.println(" <head> ");
//        out.println("     <meta charset=\"UTF-8\"> ");
//        out.println("     <title>员工列表</title> ");
//        out.println(" </head> ");
//        out.println(" <body> ");
//        out.println(" <h1>员工列表</h1> ");
//        out.println(" <a href=\"\">添加员工</a> ");
//        out.println(" <hr> ");
//        out.println(" <table border=\"1\"> ");
//        out.println("     <tr> ");
//        out.println("         <th>编号</th> ");
//        out.println("         <th>姓名</th> ");
//        out.println("         <th>工资</th> ");
//        out.println("         <th>操作</th> ");
//        out.println("     </tr> ");


        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        List<Emp> emps = new ArrayList<>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "root");
            String sql = new StringBuffer()
                    .append(" select id,name,salary ")
                    .append(" from t_emp ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Emp emp = new Emp();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setSalary(rs.getDouble("salary"));
                emps.add(emp);
//                out.println("     <tr> ");
//                out.println("         <td>" + rs.getInt("id") + "</td> ");
//                out.println("         <td>" + rs.getString("name") + "</td> ");
//                out.println("         <td>" + rs.getDouble("salary") + "</td> ");
//                out.println("         <td> ");
//                out.println("             <a href=\"\">修改</a> ");
//                out.println("             <a href=\"\">删除</a> ");
//                out.println("         </td> ");
//                out.println("     </tr> ");
            }
            request.setAttribute("emps",emps);
            // 1.转发
            // 转发属于服务器内部控制权的转移,只能是同一个服务器中的资源
            // 因此,其地址的值只需要写当前项目中的资源即可,不需要应用名
            request.getRequestDispatcher("/ems/empList.jsp").forward(request,response);
            // 2.重定向
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

//        out.println(" </table> ");
//        out.println(" </body> ");
//        out.println(" </html> ");

    }

}
